package com.googlecode.pwlodars.piteststat.database;

import java.util.Arrays;
import java.util.List;

import org.hamcrest.Description;

import com.googlecode.pwlodars.piteststat.content.PackageInfo;
import com.googlecode.pwlodars.piteststat.tests.SmartBaseMatcher;

public class PackageInfoMatcher extends SmartBaseMatcher<List<PackageInfo>> {

	private String[] expectedPackages;

	private List<PackageInfo> recivedPackages;
	
	public PackageInfoMatcher(String[] packages) {
		this.expectedPackages=packages;
	}

	@SuppressWarnings("unchecked")
	public void describeTo(Description description) {
		description.appendText("expected : "+Arrays.asList(expectedPackages)+" but received :"+Arrays.asList(recivedPackages));
	}

	@Override
	public boolean performConditionChecking(List<PackageInfo> packagesInTestContext) throws Exception {
		
		recivedPackages=packagesInTestContext;
		
		if(expectedPackages.length!=recivedPackages.size()){
			return false;
		}
		
		for (int i = 0; i < expectedPackages.length; i++) {
			boolean arePackagesTheSame = recivedPackages.get(i).getName().equals(expectedPackages[i]);
			if(!arePackagesTheSame){
				return false;
			}
		}
		
		return true;
	}

	
	public static PackageInfoMatcher hasPackages(String... packages){
		return new PackageInfoMatcher(packages);
	}

}
